<?php

namespace common\models;

use common\base\BModel;
use Yii;
class UserIpModel extends BModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user_ip';
    }

    /**
     * @db
     */
    public static function getDb()
    {
        return Yii::$app->get('db');
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'time','source'], 'integer'],
            [['ip','address','browser','os'], 'string', 'max' => 40],
            [['ip','source'], 'required', 'on' => ['add'], 'message' => '{attribute}不能为空'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'ip' => 'ip地址',
            'time' => '访问时间',
            'source'=>'来源',
            'address'=>'地址',
        ];
    }

    /**
     * 保存前
     * @param bool $insert
     * @return bool
     */
    public function beforeSave($insert)
    {
        if(parent::beforeSave($insert)){
            $time = time();
            if($this->isNewRecord){
                $this->time = $time;
            }
            return true;
        }
        return false;
    }

}
